import com.lefu.utils.common.excelhelper.ExcelWrite;
import com.lefu.utils.common.excelhelper.Head;
import com.lefu.utils.common.exception.LefuException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

/**
 * 爬 分类
 * Created by lrs on 2018/2/21.
 */
public class Category {
    private String homeURL = ""; // 根网址

    private String oneCategoryTitle = ""; // 一级分类标题

    private String twoCategoryTitle = ""; // 二级分类标题
    private String twoCategoryUrl = ""; // 二级分类URL

    /**
     * 构造函数
     * @param homeURL
     */
    public Category(String homeURL) {
        this.homeURL = homeURL;
    }

    /**
     * 创建分类excel
     * @return excel对象
     * @throws com.lefu.utils.common.exception.LefuException
     */
    private ExcelWrite createCategoryExcel() throws LefuException {
        ExcelWrite excelWrite = new ExcelWrite();
        Head head;
        // 一级分类
        head = new Head(0, "一级分类", 25, null);
        excelWrite.addHead(head);
        // 二级分类
        head = new Head(1, "二级分类", 25, null);
        excelWrite.addHead(head);
        // 二级分类 URL
        head = new Head(2, "二级分类 URL", 50, null);
        excelWrite.addHead(head);
        // 三级分类
        head = new Head(3, "三级分类", 25, null);
        excelWrite.addHead(head);
        // 三级分类 URL
        head = new Head(4, "三级分类 URL", 70, null);
        excelWrite.addHead(head);
        // 三级分类 ID
        head = new Head(5, "LsTypeSysNo", 12, null);
        excelWrite.addHead(head);
        // 二级分类 ID
        head = new Head(6, "SysNo", 8, null);
        excelWrite.addHead(head);

        return excelWrite;
    }

    /**
     * 向excel中添加一行数据
     * @param excelWrite excel对象
     * @param args 添加的数据
     * @return
     */
    private ExcelWrite writeCategoryData(ExcelWrite excelWrite, String... args) {
        excelWrite.addBody(args);
        return excelWrite;
    }

    /**
     * 爬 分类
     * @throws IOException
     * @throws LefuException
     */
    public void spiderCategory() throws IOException, LefuException {
        // 创建分类excel
        FileOutputStream fos = new FileOutputStream("./output/category.xls");
        ExcelWrite excelWrite = this.createCategoryExcel();

        Document doc = Jsoup.connect(this.homeURL).get(); // 加载AS ONE首页

        Elements allCategory= doc.select(".pros ul li"); // 通过选择器得到全部产品分类
        for (Element oneCategory : allCategory) {
            oneCategoryTitle = oneCategory.select("a").first().text(); // 获得一级分类标题
            excelWrite = writeCategoryData(excelWrite, oneCategoryTitle);

            Elements twoCategorys = oneCategory.select("table tr"); // 通过选择器得到二级分类
            for (Element twoCategory : twoCategorys) {
                // 获得二级分类标题和URL
                Element two = twoCategory.select("a").first();
                twoCategoryTitle = two.attr("title");
                twoCategoryUrl = two.attr("href").replace("../", "");

                excelWrite = writeCategoryData(excelWrite, oneCategoryTitle, twoCategoryTitle, this.homeURL+twoCategoryUrl);

                // 得到三级分类
                Elements threeCategorys = twoCategory.select("a");
                if (threeCategorys.size() != 1) {
                    for (int i=1; i<threeCategorys.size(); i++) {
                        Element threeCategory = threeCategorys.get(i);

                        Map<String, String> map = URLHelp.urlSplit(threeCategory.attr("href")); // 解析出url参数中的键值对
                        excelWrite = writeCategoryData(excelWrite, oneCategoryTitle,twoCategoryTitle, this.homeURL+twoCategoryUrl, threeCategory.attr("title"), this.homeURL+threeCategory.attr("href").replace("../", ""), map.get("LsTypeSysNo"), map.get("SysNo"));
                    }
                }
            }

            // 循环下一个一级分类前清除变量
            oneCategoryTitle = "";
            twoCategoryTitle = "";
            twoCategoryUrl = "";
        }

        // 分类爬完后关闭excel文件
        excelWrite.write(fos);
        fos.close();
    }

}
